package lxs.tool.liberator.work.service.imp;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lxs.tool.liberator.common.base.result.Response;
import lxs.tool.liberator.common.utils.IDCreator;
import lxs.tool.liberator.work.mapper.ClassMapper;
import lxs.tool.liberator.work.mapper.ParamMapper;
import lxs.tool.liberator.work.mapper.ProjectMapper;
import lxs.tool.liberator.work.param.in.MultiOptions;
import lxs.tool.liberator.work.param.in.ParamIn;

import lxs.tool.liberator.work.param.out.ParamTree;
import lxs.tool.liberator.work.pojo.Param;
import lxs.tool.liberator.work.pojo.Project;
import lxs.tool.liberator.work.service.ParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("ParamServiceImp")
public class ParamServiceImp implements ParamService {
    @Autowired
    private ParamMapper paramMapper;
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private ClassMapper classMapper;

    @Override
    public Response showTable() {
        Project mainProject = projectMapper.getMainProject();
        return Response.send(paramMapper.showTable(mainProject.getId()));
    }

    @Override
    public Response showTree() {
        Project mainProject = projectMapper.getMainProject();
        List<ParamIn> params = paramMapper.showTable(mainProject.getId());
        Map<String, ParamIn> map = new HashMap<>();
        List<ParamIn> list = new ArrayList<>();
        for (ParamIn in : params) {
            in.setChildren(new ArrayList<>());
            map.put(in.getId(), in);
        }
        for (ParamIn in : params) {
            if (in.getBelong().equals("0")) {
                list.add(in);
            } else {
                map.get(in.getBelong()).getChildren().add(in);
            }
        }
        return Response.send(washTree(list, "0"));
    }

    private List<ParamTree> washTree(List<ParamIn> params, String parentKey) {
        List<ParamTree> list = new ArrayList<>();
        for (ParamIn in : params) {
            ParamTree tree = toTree(in, parentKey);
            if (in.getChildren().size() != 0) {
                tree.setChildren(washTree(in.getChildren(), in.getKey()));
            }
            list.add(tree);
        }
        return list;
    }

    private ParamTree toTree(ParamIn in, String parentKey) {
        ParamTree tree = new ParamTree();
        tree.setId(in.getId());
        tree.setChildren(new ArrayList<>());
        tree.setKey(in.getKey());
        tree.setName(in.getName());
        tree.setType(in.getType());
        tree.setRemark(in.getRemark());
        tree.setStatus(in.getStatus());
        tree.setPlc("请输入参数名称");
        tree.setBu("新增独立参数");
        if (tree.getKey().equals("1")) {
            if (parentKey.equals("2")) {
                tree.setLabel("成员属性");
            } else {
                tree.setLabel("独立参数");
            }
        }
        if (tree.getKey().equals("2")) {
            tree.setPlc("请输入标准的类名称");
            tree.setLabel("对象参数");
            tree.setBu("新增属性");
        }
        if (tree.getKey().equals("3")) {
            tree.setLabel("数组参数");
            tree.setBu("新增属性");
        }
        if (tree.getKey().equals("4")) {
            tree.setPlc("请输入组合标识");
            tree.setLabel("参数组合");
            tree.setBu("新增参数");
        }
        return tree;
    }

    @Override
    public Response getTypeOptions() {
        List<MultiOptions> typeOptions = classMapper.getTypeOptions(projectMapper.getMainProject().getId());
        typeOptions.addAll(paramMapper.getTypeOptions());
        return Response.send(typeOptions);
    }

    @Override
    public Response getFuncOptions() {
        return Response.send(paramMapper.getFuncOptions());
    }

    @Override
    public Response saveTree(List<ParamTree> treelist) {
        for (ParamTree tree : treelist) {
            Project mainProject = projectMapper.getMainProject();
            if (tree.getId().matches("[0-9]+")) {
                if (tree.getStatus() == 1) continue;
                Param param = new Param();
                String s = IDCreator.get("param");
                tree.setId(s);
                param.setId(s);
                param.setKey(tree.getKey());
                param.setProjectId(mainProject.getId());
                param.setName(tree.getName());
                param.setType(tree.getType());
                param.setWhat(tree.getWhat());
                param.setRemark(tree.getRemark());
                param.setInterfaceId(tree.getInterfaceId());
                param.setJson(JSON.toJSONString(tree));
                paramMapper.insert(param);
            } else {
                if (tree.getStatus() == 1) {
                    paramMapper.deleteById(tree.getId());

                }
                Param param = new Param();
                param.setId(tree.getId());
                param.setProjectId(mainProject.getId());
                param.setName(tree.getName());
                param.setRemark(tree.getRemark());
                param.setType(tree.getType());
                param.setWhat(tree.getWhat());
                param.setInterfaceId(tree.getInterfaceId());
                param.setJson(JSON.toJSONString(tree));
                paramMapper.updateById(param);
            }
        }
        return Response.success();
    }

    @Override
    public Response showAll() {
        QueryWrapper<Param> wrapper = new QueryWrapper<>();
        wrapper.eq("project_id", projectMapper.getMainProject().getId());
        List<Param> params = paramMapper.selectList(wrapper);
        List<ParamTree> treeList = new ArrayList<>();
        for (Param p : params) {
            treeList.add(JSON.parseObject(p.getJson(), ParamTree.class));
        }
        return Response.send(treeList);
    }

    @Override
    public Response deleteById(String id) {
        paramMapper.deleteById(id);
        return Response.success();
    }


    @Override
    public Response getOptions() {
        return Response.send(paramMapper.getOptions());
    }
}
